package com.jabex.manager.actions.user;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.jabex.common.to.RolTO;
import org.apache.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.jabex.common.exceptions.ErrorFunctionalException;
import com.jabex.common.exceptions.ErrorOperationalException;
import com.jabex.common.exceptions.WarningFunctionalException;
import com.jabex.common.to.MessageTO;
import com.jabex.common.to.UserTO;
import com.jabex.common.util.Constants;
import com.jabex.core.bussines.IUserBp;
import com.jabex.manager.util.ConstantsManager;

/**
 * Created by jaugusto on 21/08/14. Controlador de la gestion de usuarios.
 */
@Controller
public class UserController extends com.jabex.manager.actions.Controller {

	private UserTO userTO;
	private RolTO rolTO;
	private String passworConfirm;
	private List<UserTO> allUser;


	@Autowired
	@Qualifier(IUserBp.SERVICE_NAME)
	private IUserBp userBp = null;

	public UserController() {
		this.userTO = new UserTO();
		this.rolTO = new RolTO();
	}


	//Generated by Map
	private static Map<String,Object> allRol;
	static{
		allRol = new LinkedHashMap<String,Object>();
		allRol.put(RolTO.TYPE_ROL.SUPER_ADMNISTRATOR.name(), RolTO.TYPE_ROL.SUPER_ADMNISTRATOR); //label, value
		allRol.put(RolTO.TYPE_ROL.ADMINISTRATOR.name(), RolTO.TYPE_ROL.ADMINISTRATOR); //label, value
		allRol.put(RolTO.TYPE_ROL.MONITORING.name(), RolTO.TYPE_ROL.MONITORING); //label, value
		allRol.put(RolTO.TYPE_ROL.USER.name(), RolTO.TYPE_ROL.USER); //label, value
	}

	public Map<String,Object> getAllRolValues() {
		return allRol;
	}

	/**
	 * Metodo que valida que ambos paswword son iugales.
	 * 
	 * @return true/false
	 */
	private boolean validEqualsPassword() {
		if (userTO.getSecurity().getPassword().equals(passworConfirm)) {
			userTO.getSecurity().setCodeApp(Constants.CODE_APP_MANAGER);
			userTO.getSecurity().setNameApp(Constants.NAME_APP_MANAGER);
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Metodo para guardar un nuevo usuario.
	 *
	 * @return resultado de la operacion (success/error)
	 */
	public String saveUser() {
		String result = ConstantsManager.ERROR;
		try {
			if (validEqualsPassword()) {
				MessageTO messageTO = userBp.insertUser(userTO);
				if (messageTO.getCode().equals(Constants.OPERATION_OK)) {
					result = ConstantsManager.SUCCESS;
				}
			} else {
				super.setFunctionalErrorMessage("register.error.confirm.password");
				return null;
			}
		} catch (ErrorFunctionalException e) {
			LogManager.getLogger(getClass()).error(e.getMessage());
			super.setFunctionalErrorMessage(e);
		} catch (ErrorOperationalException eop) {
			LogManager.getLogger(getClass()).fatal(eop.getMessage());
			super.setErrorOperationalMessage(eop);
		} catch (WarningFunctionalException w) {
			LogManager.getLogger(getClass()).warn(w.getMessage());
			super.setWarnMessage(w);
		}
		return result;
	}

	/**
	 * Metodo para eliminar un usuario.
	 *
	 * @return resultado de la operacion (success/error)
	 */
	public String deleteUser(String id) {
		String result = ConstantsManager.ERROR;
		try {
			MessageTO messageTO = userBp.deleteUser(id);
			if (messageTO.getCode().equals(Constants.OPERATION_OK)) {
				result = ConstantsManager.SUCCESS;
			}
		} catch (ErrorFunctionalException e) {
			LogManager.getLogger(getClass()).error(e.getMessage());
			super.setFunctionalErrorMessage(e);
		} catch (ErrorOperationalException eop) {
			LogManager.getLogger(getClass()).fatal(eop.getMessage());
			super.setErrorOperationalMessage(eop);
		} catch (WarningFunctionalException w) {
			LogManager.getLogger(getClass()).warn(w.getMessage());
			super.setWarnMessage(w);
		}
		return result;
	}

	/**
	 * Metodo para actualizar los datos de un usuario.
	 *
	 * @return resultado de la operacion (success/error)
	 */
	public String updateUser() {
		UserTO userTO = new UserTO();
		String result = ConstantsManager.ERROR;
		try {
			if (validEqualsPassword()) {
				MessageTO messageTO = userBp.updateUser(userTO);
				if (messageTO.getCode().equals(Constants.OPERATION_OK)) {
					result = ConstantsManager.SUCCESS;
				}
			} else {
				super.setFunctionalErrorMessage("register.error.confirm.password");
				return null;
			}
		} catch (ErrorFunctionalException e) {
			LogManager.getLogger(getClass()).error(e.getMessage());
			super.setFunctionalErrorMessage(e);
		} catch (ErrorOperationalException eop) {
			LogManager.getLogger(getClass()).fatal(eop.getMessage());
			super.setErrorOperationalMessage(eop);
		} catch (WarningFunctionalException w) {
			LogManager.getLogger(getClass()).warn(w.getMessage());
			super.setWarnMessage(w);
		}
		return result;

	}

	/**
	 * Get all User
	 *
	 * @return list of Users
	 */
	public List<UserTO> getAllUser() {
		try {
			allUser = userBp.getAllUser();
		} catch (ErrorFunctionalException e) {
			LogManager.getLogger(getClass()).error(e.getMessage());
			super.setFunctionalErrorMessage(e);
		} catch (ErrorOperationalException eop) {
			LogManager.getLogger(getClass()).fatal(eop.getMessage());
			super.setErrorOperationalMessage(eop);
		} catch (WarningFunctionalException w) {
			LogManager.getLogger(getClass()).warn(w.getMessage());
			super.setWarnMessage(w);
		}
		return allUser;
	}

	public void setAllUser(List<UserTO> allUser) {
		this.allUser = allUser;
	}

	public String getPassworConfirm() {
		return passworConfirm;
	}

	public void setPassworConfirm(String passworConfirm) {
		this.passworConfirm = passworConfirm;
	}

	public UserTO getUserTO() {
		return userTO;
	}

	public void setUserTO(UserTO userTO) {
		this.userTO = userTO;
	}

	public IUserBp getUserBp() {
		return userBp;
	}

	public void setUserBp(IUserBp userBp) {
		this.userBp = userBp;
	}

	public RolTO getRolTO() {
		return rolTO;
	}

	public void setRolTO(RolTO rolTO) {
		this.rolTO = rolTO;
	}
}
